package com.sdh.onlinemarket.common.util;


import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import com.sdh.onlinemarket.entity.Contract;
import com.sdh.onlinemarket.entity.User;
import org.springframework.beans.factory.annotation.Autowired;


public class PDFTempletTicket {
    public static void fillTemplate(Contract contract, User user,String providerphone,String imageurl) {

        // 模板路径
        String templatePath = "src/main/resources/contract/contract_module.pdf";
        // 生成的新文件路径
        String newPDFPath = "src/main/resources/contract/"+contract.getAuthorid()+".pdf";
//        String newPDFPath = "src/main/resources/contract/contract_out.pdf";

        PdfReader reader;
        FileOutputStream out;
        ByteArrayOutputStream bos;
        PdfStamper stamper;
        try {
            out = new FileOutputStream(newPDFPath);// 输出流
            reader = new PdfReader(templatePath);// 读取pdf模板
            bos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();
//            System.out.println(form);
//            迭代所有form
//            数据填充
//            java.util.Iterator<String> it = form.getFields().keySet().iterator();
//            while (it.hasNext()) {
//                System.out.println(it.next());
//            }
//            System.out.println(contract.getProducearea());
            switch (contract.getProducearea()){
                case "天马":{
                    form.setField("toggle_1","On",true);
                    break;
                }
                case "茶坑":{
                    form.setField("toggle_2","On",true);
                    break;
                }
                case "东甲":{
                    form.setField("toggle_3","On",true);
                    break;
                }
                case "梅江":{
                    form.setField("toggle_4","On",true);
                    break;
                }
                case "三江":{
                    form.setField("toggle_5","On",true);
                    break;
                }
                default:{
                    form.setField("produce",contract.getProducearea());
                    break;
                }
            }
            form.setField("pruduceyear",String.valueOf(contract.getProducetime().getYear()));
            form.setField("weight",String.valueOf(contract.getWeight()));
            form.setField("years",String.valueOf(contract.getYears()));

            form.setField("trusteeshipyear",String.valueOf(contract.getTrusteeshiptime().getYear()));
            form.setField("trusteeshipmonth",String.valueOf(contract.getTrusteeshiptime().getMonthValue()));
            form.setField("trusteeshipday",String.valueOf(contract.getTrusteeshiptime().getDayOfMonth()));
            form.setField("retriveyear",String.valueOf(contract.getRetrievetime().getYear()));
            form.setField("retrivemonth",String.valueOf(contract.getRetrievetime().getMonthValue()));
            form.setField("retriveday",String.valueOf(contract.getRetrievetime().getDayOfMonth()));

            form.setField("authorid",contract.getAuthorid());
            form.setField("idnum",user.getIdnum());
            form.setField("userphone",user.getPhonenum());
            form.setField("providerphone",providerphone);
            //*******pdf打勾是这样打的
//            form.setField("toggle_1","On",true);
//            form.setField("toggle_2","On",true);

            /*
            *签名放置位置
            */
            int page1 = form.getFieldPositions("usersign_af_image").get(0).page;
            int page2 = form.getFieldPositions("provider_af_image").get(0).page;

            Rectangle signrect1 = form.getFieldPositions("usersign_af_image").get(0).position;
            Rectangle signrect2 = form.getFieldPositions("provider_af_image").get(0).position;
            float x = signrect1.getLeft();
            float y = signrect1.getBottom();
//            System.out.println(x+"---------"+y);
            Image image1 = Image.getInstance(imageurl);
            Image image2 = Image.getInstance("src/resources/images/10002.png");
            PdfContentByte under = stamper.getOverContent(page1);
            image1.scaleToFit(100,100);
            image1.setAbsolutePosition(x,y);
            under.addImage(image1);
            x = signrect2.getLeft();
            y = signrect2.getBottom();
//            System.out.println(x+"---------"+y);

            under = stamper.getOverContent(page2);
            image2.scaleToFit(70,70);
            image2.setAbsolutePosition(x,y);
            under.addImage(image2);


//            form.setField("usersign_af_image",);
            stamper.setFormFlattening(true);// 如果为false那么生成的PDF文件还能编辑，一定要设为true
            stamper.close();
            Document doc = new Document();
            PdfCopy copy = new PdfCopy(doc, out);
            doc.open();
            PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
            PdfImportedPage importPage2 = copy.getImportedPage(new PdfReader(bos.toByteArray()), 2);
            copy.addPage(importPage);
            copy.addPage(importPage2);
            doc.close();

        } catch (IOException e) {
            System.out.println("生成出错");
            System.out.println(e.getMessage());
        } catch (DocumentException e) {
            System.out.println("生成出错");
            System.out.println(e.getMessage());
        }
    }
    public static void main(String[] args){
//        fillTemplate();
    }
}
